import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService, ICoolCache } from 'midwayjs-cool-core';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/orm';

import { NewSkuDTO, ListSkuDTO } from '../dto/productClothing';
import { MallProductClothingEntity } from '../entity/productClothing';

@Provide()
export class MallProductClothingService extends BaseService {
  @InjectEntityModel(MallProductClothingEntity)
  mallProductClothingEntity: Repository<MallProductClothingEntity>;

  @Inject('cool:cache')
  coolCache: ICoolCache;

  async add(sku: NewSkuDTO) {
    return await this.mallProductClothingEntity.save(sku);
  }

  async list(op: ListSkuDTO) {
    return await this.mallProductClothingEntity
      .createQueryBuilder('pc')
      .select(['pc.poster', 'pc.color', 'pc.size', 'pc.price', 'pc.stock'])
      .where('pc.productId = :id', { id: op.productId })
      .getMany();
  }
}
